﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AlterGear.SqlIndexerUI
{
    public partial class FrmMain : Form
    {
        public SQLLibrary.SQLList sqlList;
        public SQLLibrary.SQLList SqlList
        {
            get
            {
                return this.sqlList;
            }
            set
            {
                this.sqlList = value;
                this.SetSQLList();
            }
        }
        private List<AlterGear.SQLLibrary.IndexInfo> lstIndexInfo;

        public FrmMain()
        {
            InitializeComponent();
            sqlList = new AlterGear.SQLLibrary.SQLList();
            lstIndexInfo = new List<AlterGear.SQLLibrary.IndexInfo>();
            this.FillComboList();
            //this.btnSaveConnection.Enabled = false;
            this.btnDelConnection.Enabled = false;
        }

        private void FillComboList()
        {
            List<SQLLibrary.SQLList> sqlListSaved = SQLLibrary.SQLListSaved.Deserailize().ListSaved;
            if(sqlListSaved.Count > 0)
                sqlListSaved.Insert(0, new AlterGear.SQLLibrary.SQLList());
            this.comboDatabases.DataSource = sqlListSaved;
            this.comboDatabases.DisplayMember = "DisplaySQLConn";
        }
        private void FrmMain_Load(object sender, EventArgs e)
        {
            this.BindIndexList();
        }

        private void btnSelectDB_Click(object sender, EventArgs e)
        {
            FrmSQLConnect frmSqlConnect = null;
            if (this.SqlList != null)
            {
                frmSqlConnect = new FrmSQLConnect(this.SqlList);
            }
            else
                frmSqlConnect = new FrmSQLConnect();
            DialogResult dgResult = frmSqlConnect.ShowDialog();
            if (dgResult == DialogResult.OK)
            {
                this.SqlList = frmSqlConnect.sqlList;
                //this.lblSelectedConnString.Text = this.sqlList.SQLServerName;
                //this.FetchIndices();
            }
            frmSqlConnect.Dispose();

            //MessageBox.Show(this.sqlList.ConnectionString);
        }

        private void FetchIndices()
        {
            if (!string.IsNullOrEmpty(this.sqlList.SQLServerName)
                && !string.IsNullOrEmpty(this.sqlList.SQLDatabaseName))
            {
                this.lstIndexInfo = this.sqlList.GetIndices((int)this.numericFragPct.Value);
                this.BindIndexList();
            }
        }

        private void BindIndexList()
        {
            this.indexInfoBindingSource.DataSource = this.lstIndexInfo;
            this.dataGridView1.Refresh();
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (AlterGear.SQLLibrary.IndexInfo indexInfo in this.lstIndexInfo)
            {
                indexInfo.Selected = true;
            }
            DataGridView g = new DataGridView();
            
            this.BindIndexList();
        }

        private void btnDefragSelected_Click(object sender, EventArgs e)
        {
            if (this.lstIndexInfo.Count > 0)
            {
                foreach (AlterGear.SQLLibrary.IndexInfo indexInfo in this.lstIndexInfo)
                {
                    if (indexInfo.Selected == true)
                    {
                        this.sqlList.ReorganiseIndex(indexInfo);
                    }
                }
                this.FetchIndices();
            }
        }

        private void btnAbout_Click(object sender, EventArgs e)
        {
            AboutBox abBox = new AboutBox();
            abBox.ShowDialog();
        }

        private void dataGridView1_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
        {
            if (e.Column.Name == "AvgFragPct")
            {
                if ((double)e.CellValue1 > (double)e.CellValue2)
                    e.SortResult = 1;
                else
                    e.SortResult = -1;
                e.Handled = true;
            }
            else if (e.Column.Name == "Name" || e.Column.Name == "TableName")
            {
                e.SortResult = ((string)e.CellValue1).CompareTo((string)e.CellValue2);
                e.Handled = true;
            }
            else if (e.Column.Name == "IndexId")
            {
                e.SortResult = ((int)e.CellValue1).CompareTo((string)e.CellValue2);
                e.Handled = true;
            }
        }

        private void btnFilter_Click(object sender, EventArgs e)
        {
            this.FetchIndices();
        }

        private void btnRebuildSelected_Click(object sender, EventArgs e)
        {
            if (this.lstIndexInfo.Count > 0)
            {
                foreach (AlterGear.SQLLibrary.IndexInfo indexInfo in this.lstIndexInfo)
                {
                    if (indexInfo.Selected == true)
                    {
                        this.sqlList.ReBuildIndex(indexInfo);
                    }
                }
                this.FetchIndices();
            }
        }

        private void btnLoadDatabase_Click(object sender, EventArgs e)
        {
            SQLLibrary.SQLList selected = (SQLLibrary.SQLList)this.comboDatabases.SelectedItem;
            if (!string.IsNullOrEmpty(selected.DisplaySQLConn))
            {
                this.SqlList = selected;
            }
        }

        private void SetSQLList()
        {
            this.lblSelectedConnString.Text = string.Format("[{0}].[{1}]", this.SqlList.SQLServerName, this.sqlList.SQLDatabaseName);
            this.FetchIndices();

            if (SQLLibrary.SQLListSaved.ContainsSQLList(this.SqlList))
            {
                this.btnDelConnection.Enabled = true;
            }
            else
                this.btnDelConnection.Enabled = false;
        }

        private void btnSaveConnection_Click(object sender, EventArgs e)
        {
            SQLLibrary.SQLListSaved.SaveSQLList(this.sqlList);
            this.SetSQLList();
            this.FillComboList();
        }

        private void btnDelConnection_Click(object sender, EventArgs e)
        {
            SQLLibrary.SQLListSaved.DeleteSQLList(this.sqlList);
            this.SetSQLList();
            this.FillComboList();
        }
    }
}
